﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system_Entity;
using bdnt_pets_system.Tools;
using System.Globalization;
using System.Windows.Media.Imaging;

namespace bdnt_pets_system.PageViews.Failures
{
    public partial class FailuresFrameControl : BaseUserControl
    {
        Tbl_Menu CuMenu = new Tbl_Menu();
        public FailuresFrameControl()
        {
            InitializeComponent();
            App.CurrentParentGrid = gdMainContent;
        }

        public int cumenuId = 0;
        public FailuresFrameControl(Tbl_Menu cuMenu)
        {
            InitializeComponent();
            i = 0;
            CuMenu = cuMenu;
            App.CurrentParentGrid = gdMainContent;
            if (cuMenu == null)
                return;
            if (string.IsNullOrEmpty(cuMenu.MenuModule))
                return;
            InitMenus();
        }
        int i = 0;
        public void InitMenus()
        {
            if (CuMenu == null || CuMenu.Id == 0)
                return;
            if (SessionManager.RoleAllMenus.Count == 0)
                return;
            spMenus.Children.Clear();
            var pMs = SessionManager.RoleAllMenus.Where(p => p.PMenuId == CuMenu.Id && p.MenuType == 0).OrderBy(p => p.MenuSequence);

            foreach (var item in pMs)
            {
                if (i == 0 || cumenuId == item.Id)
                {
                    object obj = Activator.CreateInstance(Type.GetType(item.MenuModule), null);
                    if (obj != null)
                    {
                        gdMainContent.Children.Clear();
                        gdMainContent.Children.Add(obj as UIElement);
                    }
                }
                //     <Rectangle MinWidth="50" Height="20" RadiusX="15" RadiusY="15" >

                //</Rectangle>

                //Rectangle cugd = new Rectangle()
                //{
                //    MinWidth = 120,
                //    Height = 30,
                //    Margin = new Thickness(20, 0, 0, 0),
                //    RadiusX = 6,
                //    RadiusY = 6
                //};
                //cugd.MouseEnter += new MouseEventHandler(cugd_MouseEnter);
                //cugd.MouseLeave += new MouseEventHandler(cugd_MouseLeave);
                
                HyperlinkButton hlb = new HyperlinkButton()
                {
                    Tag = item,
                    Foreground = new SolidColorBrush(Colors.Black),
                    FontSize = 13,
                    Margin = new Thickness(20, 0, 0, 0),
                    VerticalAlignment = System.Windows.VerticalAlignment.Center
                };

                if (i == 0 || cumenuId == item.Id)
                {
                    hlb.FontSize = 14;
                    hlb.Foreground = new SolidColorBrush(HelpTools.HtmlToColor("#2C4F8F"));
                    hlb.FontWeight = FontWeights.Bold;
                    //cugd.Fill = new SolidColorBrush(HelpTools.HtmlToColor("#2B4471"));
                }

                hlb.Content = SessionManager.IsCN_ZH ? item.MenuName : item.en_MenuName;
                hlb.Click += new RoutedEventHandler(hlb_Click);
                spMenus.Children.Add(hlb);
                i++;
            }
        }

        //void cugd_MouseLeave(object sender, MouseEventArgs e)
        //{

        //    var cuo = sender as Rectangle;
        //    cuo.Fill = new SolidColorBrush(HelpTools.HtmlToColor("#2B4471"));
        //}

        //void cugd_MouseEnter(object sender, MouseEventArgs e)
        //{
        //    var cuo = sender as Rectangle;
        //    cuo.Fill = new SolidColorBrush(HelpTools.HtmlToColor("#999999"));
        //}

        void hlb_Click(object sender, RoutedEventArgs e)
        {
            var cuDt = (sender as HyperlinkButton).Tag as Tbl_Menu;
            if (cuDt == null)
                return;
            if (string.IsNullOrEmpty(cuDt.MenuModule))
                return;
            cumenuId = cuDt.Id;
            InitMenus();
            object obj = Activator.CreateInstance(Type.GetType(cuDt.MenuModule), null);
            if (obj != null)
            {
                gdMainContent.Children.Clear();
                gdMainContent.Children.Add(obj as UIElement);
            }
        }
    }
}
